/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.webos.java.rssreader;

import org.json.JSONObject;
import org.webos.java.base.Application;
import org.webos.java.base.ApplicationType;
import org.webos.java.base.Session;
import org.webos.java.base.SystemManager;
import org.webos.java.base.hsqldb.HSQLDBConnection;
import org.webos.java.base.hsqldb.migration.DBDefinition;
import org.webos.java.base.hsqldb.migration.RowDefinition;
import org.webos.java.base.hsqldb.migration.TableDefinition;
import org.webos.java.base.workspace.FrameHolder;
import org.webos.java.base.workspace.Splitter;
import org.webos.java.base.workspace.Workspace;
import org.webos.java.root.Log;

/**
 *
 * @author Kostya
 */

@ApplicationType(code="rssreader", jsClass="rssreader.Application",
        name="RSS feed reader", errorCode=1214, icon="rss_icn rss_icn_rss")
public class RSSReader extends Application{

    final HSQLDBConnection connection = new HSQLDBConnection("/.rssreader/feeds", "sa", "");
    Splitter splitter = new Splitter("main", true, 0.3);
    Workspace workspace = new Workspace(this, "main", splitter);
    FrameHolder treeHolder = null;
    FrameHolder mainHolder = null;
    FoldersFeeds foldersFeeds = null;
    Entries entries = null;
    JSONObject options = new JSONObject();
    int allUnread = -1;

    public RSSReader(Session aSession) {
        super(aSession);
    }

    @Override
    public void initialize() {
        options = getOptionObject("options");
        if(!setupDB()){
            sendError(1201, "Can't setup DB. Application closed");
            stopApplication(null);
            return;
        }
        SystemManager.setupProxy(getSession());
        foldersFeeds = new FoldersFeeds(this);
        entries = new Entries(this);
        treeHolder = new FrameHolder(splitter, "foldersFeeds", foldersFeeds);
        mainHolder = new FrameHolder(splitter, "entries", entries);
        splitter.setLeftItem(treeHolder);
        splitter.setRightItem(mainHolder);
        addWorkspace(workspace);
        addFrame(foldersFeeds);
        addFrame(entries);
        feedsChanged();
        showWorkspace(workspace);
        feedsChanged();
    }



    private boolean setupDB(){
        try {
            if(!connection.openConnection(getSession(), false))
                return false;
            DBDefinition dbd = new DBDefinition();
            TableDefinition folders = new TableDefinition("FOLDERS");
            RowDefinition idFolder = new RowDefinition("ID", "INTEGER", true);
            folders.addRow(idFolder);
            folders.addRow(new RowDefinition("NAME", "VARCHAR(255)"));
            dbd.addTable(folders);

            TableDefinition feeds = new TableDefinition("FEEDS");
            RowDefinition idFeed = new RowDefinition("ID", "INTEGER", true);
            feeds.addRow(idFeed);
            feeds.addRow(new RowDefinition("NAME", "VARCHAR(255)"));
            feeds.addRow(new RowDefinition("URL", "VARCHAR(255)"));
            feeds.addRow(new RowDefinition("UPDATE_MIN", "INTEGER", "0"));
            feeds.addRow(new RowDefinition("ID_FOLDER", "INTEGER", idFolder));
            feeds.addRow(new RowDefinition("UPDATE_TIME", "TIMESTAMP"));
            dbd.addTable(feeds);

            TableDefinition entries = new TableDefinition("ENTRIES");
//            entries.setType("TEXT");
            RowDefinition idEntry = new RowDefinition("ID", "INTEGER", true);
            entries.addRow(idEntry);
            entries.addRow(new RowDefinition("URI", "VARCHAR(255)"));
            entries.addRow(new RowDefinition("URL", "VARCHAR(255)"));
            entries.addRow(new RowDefinition("TITLE", "VARCHAR(511)"));
            entries.addRow(new RowDefinition("BODY", "VARCHAR(20000)"));
            entries.addRow(new RowDefinition("PUBLISH_TIME", "TIMESTAMP"));
            entries.addRow(new RowDefinition("FETCH_TIME", "TIMESTAMP"));
            entries.addRow(new RowDefinition("BOOKMARK", "BOOLEAN", "false"));
            entries.addRow(new RowDefinition("READ", "BOOLEAN", "false"));
            entries.addRow(new RowDefinition("ID_FOLDER", "INTEGER", idFolder));
            entries.addRow(new RowDefinition("ID_FEED", "INTEGER", idFeed));
            dbd.addTable(entries);

            if(!dbd.migrateDB(connection.getConnection()))
                return false;
        } catch (Throwable t) {
            Log.error(this, "Exception thrown: " + t.getMessage());
            t.printStackTrace();
        }
        return true;
    }

    void feedsChanged(){
        int unread = 0;
        for (int i = 0; i < foldersFeeds.controller.getChildrenCount(); i++) {
            FoldersFeeds.FolderItem item = foldersFeeds.controller.getChild(i);
            unread += item.unreadCount;
        }
        if(unread!=allUnread){
            allUnread = unread;
            String title = "RSS reader";
            if(allUnread>0)
                title = "["+allUnread+"] "+title;
            workspace.setCaption(title);
            setName(title);
        }
    }
}
